package bd2_xml.manipulacaoXML;

import bd2_xml.persistencia.Selecionar;
import bd2_xml.visual.Principal;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.XMLOutputter;
import java.sql.ResultSet;

public class CriarXML {

    public static void criarXML(){
        //Cria o elemento datapacket
        Element datapacket = new Element("DATAPACKET");
        //Define datapacket como root 
        Document documento = new Document(datapacket);
        //Cria o elemento metadata
        Element metadata = new Element("METADATA");
        //Cria o elemento fields
        Element fields = new Element("FIELDS");
        //Cria o elemento field
        ResultSetMetaData rsmd = null;
        try {
            rsmd = Selecionar.getMetaData(Principal.TFNometabela.getText());
        } catch (SQLException ex) {
            Logger.getLogger(CriarXML.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            for (int i = 1; i <=rsmd.getColumnCount(); i++) {
                Element field = new Element("FIELD");
                try {
                    if (rsmd.isNullable(i) == 0) {
                        field.setAttribute("required", "true");
                        System.out.println("NOT NULL");
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(CriarXML.class.getName()).log(Level.SEVERE, null, ex);
                }
                if ((!rsmd.getColumnTypeName(i).equals("INTEGER")) && (!rsmd.getColumnTypeName(i).equals("DATE"))) {
                    
                    if (rsmd.getColumnTypeName(i).equals("NUMERIC")) {
                        field.setAttribute("DECIMALS", String.valueOf(rsmd.getScale(i)));
                        System.out.println("Precisão: " + rsmd.getScale(i));
                    }
                    field.setAttribute("WIDTH", String.valueOf(rsmd.getPrecision(i)));
                    System.out.println("Tamanho: " + rsmd.getPrecision(i));
                }
                
                try {
                    field.setAttribute("fieldtype", rsmd.getColumnTypeName(i));
                    System.out.println("Tipo: " + rsmd.getColumnTypeName(i)); 
                    field.setAttribute("attname", rsmd.getColumnLabel(i));
                    System.out.println("Nome: " + rsmd.getColumnLabel(i));                                   

                } catch (SQLException ex) {
                    Logger.getLogger(CriarXML.class.getName()).log(Level.SEVERE, null, ex);
                }

                fields.addContent(field);

            }
        } catch (SQLException ex) {
            Logger.getLogger(CriarXML.class.getName()).log(Level.SEVERE, null, ex);
        }

        metadata.addContent(fields);
        Element rowdata = new Element("ROWDATA");
        try {
            ResultSet rs = Selecionar.getAllDate(Principal.TFNometabela.getText());
            while(rs.next()){
                Element row = new Element("ROW");
                for(int i=rs.getMetaData().getColumnCount(); i>0;i--){
                    System.out.print("Nome: "+rs.getMetaData().getColumnName(i) );
                    System.out.println(" Dado: "+rs.getNString(i));
                    if (rs.getNString(i)!=null){
                        row.setAttribute(rs.getMetaData().getColumnName(i).toString(), rs.getNString(i));
                    }
                    
                }
                rowdata.addContent(row);
            }
        } catch (SQLException ex) {
            Logger.getLogger(CriarXML.class.getName()).log(Level.SEVERE, null, ex);
        }

        
        datapacket.addContent(metadata);
        datapacket.addContent(rowdata);

        XMLOutputter xout = new XMLOutputter();
        

        try {

            //Criando o arquivo de saida

            FileWriter arquivo = new FileWriter(
                    new File(Principal.TFXML.getText()));

            //Imprimindo o XML no arquivo
            
            Principal.TFSaida.setText(xout.outputString(documento));
            
            xout.output(documento, arquivo);

        } catch (IOException e) {

            e.printStackTrace();

        }
    }
}
